<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\care;


use app\dao\care\ServiceRemindDao;
use app\services\BaseServices;
use crmeb\services\FormBuilder;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Route;

class ServiceRemindServices extends BaseServices
{
    public function __construct(ServiceRemindDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where,bool $is_page = false){
        $page = $limit = 0;
        if($is_page) [$page,$limit] = $this->getPageValue();
        $list = $this->dao->getList($where,$page,$limit,$is_page);
        foreach ($list['list'] as &$item){
            $item['start_time'] = $item['start_time'] ? date('H:i',$item['start_time']) : '未设置';
            $item['end_time'] = $item['end_time'] ? date('H:i',$item['end_time']) : '未设置';
        }
        return $list;
    }

    /**
     * 添加/修改
     * @param int $id
     * @return array
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(int $id){
        $info = $this->dao->get($id);
        $place = Db::name('base_place')->field('id as value,title as label')->where(['is_del'=>0,'status'=>1])->order('sort desc')->select()->toArray();
        $field = array();
        $title = $id ? '修改' : '添加';
        $field[] = FormBuilder::hidden('id', $info['id']??0);
        $field[] = FormBuilder::select('place_id','关联地点',$info['place_id']??'')->options($place);
        $field[] = FormBuilder::time('start_time', '开始时间', $info['start_time'] ? date('H:i',$info['start_time']) : '')->format('HH:mm')->style(['width'=>'100%']);
        $field[] = FormBuilder::time('end_time', '结束时间', $info['end_time'] ? date('H:i',$info['end_time']) : '')->format('HH:mm')->style(['width'=>'100%']);
        $field[] = FormBuilder::input('phone','联系电话',$info['phone']??'');
        $field[] = FormBuilder::radio('status','是否启用',$info['status']??1)->options([['value'=>1,'label'=>'启用'],['value'=>0,'label'=>'禁用']]);
        return create_form($title,$field,Route::buildUrl('care/remind/save'),'POST');
    }

    /**
     * 保存
     * @param int $id
     * @param array $data
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(int $id,array $data){
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if($id){
            if(!$this->dao->get($id)) throw new ValidateException('数据不存在');
            if(!$this->dao->update($id,$data)) throw new ValidateException('提交失败或者您没有修改什么');
        }else{
            unset($data['id']);
            $data['add_time'] = time();
            if(!$this->dao->save($data)) throw new ValidateException('提交失败');
        }
        return '提交成功';
    }
    /**
     * 显示隐藏
     * @param int $id
     * @param int $status
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setShow(int $id, int $status){
        $info = $this->dao->get($id);
        if (!$info) throw new ValidateException('数据不存在');
        if (!$this->dao->update($id, ['status' => $status])) throw new ValidateException($status == 1 ? '启用失败' : '禁用失败');
        return $status == 1 ? '启用成功' : '禁用成功';
    }

    /**
     * 删除
     * @param int $id
     * @return string
     */
    public function delete(int $id){
        $res = $this->dao->update($id,['is_del'=>1,'delete_time'=>time()]);
        if(!$res) throw new ValidateException('删除失败,请稍候再试');
        return '删除成功';
    }

    /**
     * 获取地点提醒
     * @param int $place_id
     * @param string $time
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPlaceList(int $place_id,string $time){
        $list = Db::name('service_remind')->where(['is_del'=>0,'status'=>1,'place_id'=>$place_id])->select()->toArray();
        $remindArr = array();
        foreach ($list as &$item){
            $item['start_time'] = date('Y-m-d H:i',strtotime(date('H:i',$item['start_time'])));
            $item['end_time'] = date('Y-m-d H:i',strtotime(date('H:i',$item['end_time'])));
            if($item['start_time'] > $item['end_time']) $item['end_time'] = date('Y-m-d H:i',strtotime($item['end_time'].'+1 day'));
            if($item['start_time'] == $item['end_time']) $remindArr[] = ['id'=>$item['id'],'phone'=>$item['phone']];
            if($item['start_time'] <= $time && $item['end_time'] >= $time) $remindArr[] = ['id'=>$item['id'],'phone'=>$item['phone']];
        }
        return $remindArr;
    }
}